import type { Ref } from 'vue';
import type { Router } from 'vue-router';
import { getRouteGuards, getRoutes, runStartup } from '@peng_kai/kit/admin/defines';
import { JsBridge } from './JsBridge';

export { defineRoute, defineRouteGuard, defineStartup } from '@peng_kai/kit/admin/defines';
export { getRouteGuards, getRoutes, runStartup };

getRoutes.modules = import.meta.glob('~/**/*route.ts', { eager: true });
getRouteGuards.modules = import.meta.glob('~/**/*route-guard.ts', { eager: true });
runStartup.modules = import.meta.glob('~/**/*.startup.ts');

export const jsBridge = new JsBridge();
export const appValues = {
  get language() {
    if (Telegram.WebApp.initData) {
      try {
        const tgParams = new URLSearchParams(decodeURIComponent(Telegram.WebApp.initData));
        const userData = JSON.parse(tgParams.get('user') || '{}');
        return userData?.language_code || 'en-US';
      }
      catch {}
    }

    return navigator.language;
  },
} as {
  language: string;
  router?: Router;
  routeKey?: Ref<number>;
  refreshRoute?: () => void;
};

if (import.meta.env.DEV)
  (window as any).appValues = appValues;
